package com.zz.inventory.controller;

import java.util.List;
import java.io.IOException;

import com.alibaba.nacos.api.model.v2.Result;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.zz.inventory.domain.dto.ReviewScoreDTO;
import com.zz.inventory.domain.dto.StudentScoreQuery;
import com.zz.inventory.domain.dto.StudentWorstScoreDTO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.zz.inventory.domain.Students;
import com.zz.inventory.service.IStudentsService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 学生信息Controller
 * 
 * @author ruoyi
 * @date 2025-07-01
 */
@Slf4j
@RestController
@RequestMapping("/students")
public class StudentsController extends BaseController
{
    @Autowired
    private IStudentsService studentsService;

    /**
     * 查询学生信息列表
     */
    @RequiresPermissions("Teacher:students:list")
    @GetMapping("/list")
//    public AjaxResult worstScores() {
//        startPage();
//        List<StudentWorstScoreDTO> list = studentsService.selectStudentWorstScores();
//        return success(list);
//    }
    public Result getWorstScores(StudentScoreQuery query) {
        PageInfo<StudentWorstScoreDTO> pageInfo = studentsService.selectStudentWorstScores(query);
        return Result.success(pageInfo);
    }
    /**
     * 导出学生信息列表
     */
    @RequiresPermissions("Teacher:students:export")
    @Log(title = "学生信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Students students)
    {
        List<Students> list = studentsService.selectStudentsList(students);
        ExcelUtil<Students> util = new ExcelUtil<Students>(Students.class);
        util.exportExcel(response, list, "学生信息数据");
    }

    /**
     * 获取学生信息详细信息
     */
    @RequiresPermissions("Teacher:students:query")
    @GetMapping(value = "/{studentId}")
    public AjaxResult getInfo(@PathVariable("studentId") Long studentId)
    {
        return success(studentsService.selectStudentsByStudentId(studentId));
    }

    /**
     * 新增学生信息
     */
    @RequiresPermissions("Teacher:students:add")
    @Log(title = "学生信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Students students)
    {
        return toAjax(studentsService.insertStudents(students));
    }

    /**
     * 修改学生信息
     */
    @RequiresPermissions("Teacher:students:edit")
    @Log(title = "学生信息", businessType = BusinessType.UPDATE)
    @PostMapping("/review")
    public Result<String> review(@RequestBody ReviewScoreDTO dto, HttpServletRequest request) {
        log.info("收到复核请求 - 记录ID: {}, 学生ID: {}, 考试ID: {}",
                 dto.getStudentId(), dto.getExamId());
        if (dto.getExamId() == null || dto.getStudentId() == null) {
            log.error("缺少关键ID参数");
            return Result.failure("缺少关键ID参数");
        }
        try {
            // 设置复核人(当前登录用户)
            dto.setReviewer(SecurityUtils.getUsername());

            int result = studentsService.reviewScore(dto);
            if (result > 0) {
                return Result.success("复核成功");
            }
            return Result.failure("复核失败，记录不存在");
        } catch (IllegalArgumentException e) {
            return Result.failure(e.getMessage());
        }
    }

    /**
     * 删除学生信息
     */
    @RequiresPermissions("Teacher:students:remove")
    @Log(title = "学生信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{studentIds}")
    public AjaxResult remove(@PathVariable Long[] studentIds)
    {
        return toAjax(studentsService.deleteStudentsByStudentIds(studentIds));
    }
}
